<?php
header("content-type:text/html;charset=utf-8");
include "../../public/config/config.php";
include("../../public/include/page_tel.class.php");
include "../../public/include/funcs.php";
$my="active";
$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
$self=substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1);
$obj=new page($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
$tel_type=select($link, "tel_type");

if($_POST['add_tel']=="add_tel"){
	$add_arr['tid']=$_POST['se_tid'];
	$add_arr['is_here']=1;
	$add_arr['uname']=$_POST['uname'];
	$add_arr['tel']=$_POST['tel'];
	$add_arr['create_time_int']=time();
	$re=add($link, "tels", $add_arr);
	if($re){
		echo "<script>";
		echo "alert('添加成功！');";
		echo "</script>";
	}
}

if($_POST['save_tel']=="save_tel"){
	foreach($_POST as $k=>$v){
		$k_name=substr($k, 0,strrpos($k, "-"));
		$j=substr($k, (strrpos($k, "-")+1));
//		echo $k_name."---".$j."<br>";
		if(intval($j) != 0){
			$save_tel_arr[$j][$k_name]=$v;
			if(!$save_tel_arr[$j]['save_time_int']){
				$save_tel_arr[$j]['save_time_int']=time();
			}
		}
	}
	foreach($save_tel_arr as $v){
		$save_id=$v['id'];
		unset($v['id']);
		save($link, "tels", $v,"id=$save_id");
	}
echo "<script>";
echo "alert('修改成功！');";
echo "</script>";
/*echo "<pre>";
print_r($_POST);
print_r($save_tel_arr);
echo "</pre>";*/
}


$tid=$_REQUEST['tid'];
$is_here=$_REQUEST['is_here'];
$page=$_REQUEST['page'];
$type=$_REQUEST['type'];
$keyword=$_REQUEST['keyword'];
$arr=$obj->listData("tels",10,$tid,$is_here,$type,$keyword,$page);
$sel_arr[$type]="selected";
$href0=$self."?tid=".$tid."&is_here=0";
$href1=$self."?tid=".$tid."&is_here=1";
if($tid){
	$typeName=select($link, "tel_type","id=$tid");
}else{
	$typeName['0']['tname']="所有数据";
}

?>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
	<title><?php echo $typeName['0']['tname'];?>通讯集 -- 后台管理</title>
	
	<!-- Bootstrap -->
	<link href="/public/css/bootstrap.min.css" rel="stylesheet">
	<link href="/public/css/main.css" rel="stylesheet" type="text/css"/>
	<link rel="stylesheet" href="tel.css" />
	
	<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
	<!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
	<!--[if lt IE 9]>
	<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
	<![endif]-->
</head>
<body>
<?php //include "../../public/include/head_admin.php";?>
<div class="container bg_liter tab">
	<h3 class="text-center bg-primary lhei"><?php echo $typeName['0']['tname'];?>通讯集 -- 后台管理</h3>
	<form action="<?php echo $self;?>" method="post">
		<input type="hidden" name="tid" value="<?php echo $tid;?>" />
		<input type="hidden" name="is_here" value="<?php echo $is_here;?>" />
		<select name="type" class="form-control w80 in_bl">
			<option <?php echo $sel_arr['uname']?> value="uname">姓名</option>
			<option <?php echo $sel_arr['tel']?> value="tel">电话</option>
		</select>
		关键字：<input type="text" name="keyword" class="form-control w200 in_bl" value="<?php echo $keyword;?>" />
		<input type="submit" class="btn btn-info" value="搜索"/>
	</form>
	<span><a href="<?php echo $href0;?>" class="btn btn-danger" >查看离职员工</a></span>
	<span><a href="<?php echo $href1;?>" class="btn btn-primary" >查看在职员工</a></span>
	<span class="btn btn-success" id="add_tel" >添加联系人</span>
	<br /><br />
	<div class="add_tel" id="add_tel_div">
		<form class="form-inline" action="<?php echo $self;?>" method="post">
			<input type="hidden" name="add_tel" value="add_tel" />
			<input type="hidden" name="tid" value="<?php echo $tid;?>" />
			<input type="hidden" name="page" value="<?php echo $page;?>" />
			<input type="hidden" name="is_here" value="<?php echo $is_here;?>" />
			<input type="hidden" name="type" value="<?php echo $type;?>" />
			<input type="hidden" name="keyword" value="<?php echo $keyword;?>" />
			姓名：<input type="text" class="form-control" name="uname" value="" />
			电话：<input type="text" class="form-control" name="tel" value="" />
			分类：<select name="se_tid" class="form-control">
<?php
foreach($tel_type as $v){
?>
				<option <?php if($v['id']==$tid){echo "selected";}?> value="<?php echo $v['id'];?>"><?php echo $v['tname'];?></option>
<?php
}
?>
			</select>
			<input type="submit" class="btn btn-success" value="提交添加"/>
		</form>
	</div>
	<form action="<?php echo $self;?>" method="post">
	<input type="hidden" name="save_tel" value="save_tel" />
	<input type="hidden" name="tid" value="<?php echo $tid;?>" />
	<input type="hidden" name="page" value="<?php echo $page;?>" />
	<input type="hidden" name="is_here" value="<?php echo $is_here;?>" />
	<input type="hidden" name="type" value="<?php echo $type;?>" />
	<input type="hidden" name="keyword" value="<?php echo $keyword;?>" />
	<table class="table table-hover bg-info"  border="2" cellspacing="0" cellpadding="0">
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>电话</th>
			<th>是否在职</th>
			<th>操作</th>
		</tr>
<?php
$i=0;
foreach($arr as $v){
$i++;
?>			
		<tr>
			<input type="hidden" name="id-<?php echo $i;?>" value="<?php echo $v['id'];?>" />
			<td><?php echo $i;?></td>
			<td><input class="form-control w_uname" type="text" name="uname-<?php echo $i;?>" value="<?php echo $v['uname'];?>" /></td>
			<td><input class="form-control w_tel" type="text" name="tel-<?php echo $i;?>" value="<?php echo $v['tel'];?>" /></td>
			<td><input class="form-control w_is_here" type="text" name="is_here-<?php echo $i;?>" value="<?php echo $v['is_here'];?>" /></td>
			<td>
				<a class="btn btn-danger" onclick="if(confirm('确定删除吗？')) return true; else return false;"
				href="del_tel.php?id=<?php echo $v['id'];?>&tid=<?php echo $tid;?>&is_here=<?php echo $is_here;?>&page=<?php echo $page;?>&type=<?php echo $type;?>&keyword=<?php echo $keyword;?>">删除</a>
				<a href="tel:<?php echo $v['tel'];?>" class="btn btn-success">拨打</a>
			</td>
		</tr>
<?php
}
?>	
		<tr>
			<td colspan="5">
	<input class="form-control btn-info w200 in_bl" type="submit" value="提交修改"/>
	</form>
			</td>
		</tr>
		<tr>
			<td colspan="5">
				<?php echo $obj->showPage();?>
			</td>
		</tr>		
	</table>
	
</div>
<div class="hei_50"></div>
<?php include "../../public/include/footer_admin.php";?>
<script type="text/javascript">
document.getElementById("add_tel").onclick=function(){
	document.getElementById("add_tel_div").style.display="block";
}
</script>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script type="text/javascript" src="/public/js/jquery-3.3.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/public/js/footer.js"></script>
</body>
</html>